<?php
Zend_Loader::loadClass('Lead_Document');
Zend_Loader::loadClass('Led_Entity_Renderer');
Zend_Loader::loadClass('Led_Entity_Collection');
/**
 * LED application
 *
 */
class Led_Application
{
    private static $_instance;
    
    /**
     * DB
     *
     * @var Zend_Db_Adapter_Pdo_Mysql
     */
    private $_db;
    
    /**
     * EDO handler
     *
     * @var Lead_EDO_Handler_Functional
     */
    private $_edo;
    
    private $_user;
    
        
    
    private $_saferoot;
    private $_exporoot;
    private $_retailroot;
    
    
    private $_pageid;
    
    private $_page;
    
    private $_langid = 2;
    private $_appid = 2;
    
    private $_document;
    
     /**
     * Singleton instance
     * 
     * @return Led_Application
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    
    
    private function __construct()
    {
        session_name('led');
        session_start();
                  
        $conf = array (
                'host'     => '127.0.0.1',
                 'username' => 'root',
                 'password' => 'savethemanatee',
                 'dbname'   => 'dporssi2');
        
        Zend_Loader::loadClass('Zend_Db');
        $this->_db = Zend_Db::factory('PDO_MYSQL', $conf);
        
        
        
                
        Zend_Loader::loadClass('Lead_EDO');
        $backend = array(
            'backend' => 'Memcached',
            'backendOptions' => array('servers' => array('host' => 'localhost','port' => 11211, 'persistent' => true)),
        );
        
        $this->_edo = Lead_EDO::factory($this->getDb(), $backend);
        
        $this->_edo->setAttribute('Callback_GetDimension', array($this, 'getAppid'));
        $this->_edo->addSearchPath('/wwwroot/LEAD/library/Led/Entities');
        
        $this->_edo->addTable('led_tree');
        
        $this->_user = $this->getEdo()->get('ent_user');
        
        if(isset($_SESSION['user']))
            $this->_user->fetch($_SESSION['user']);
        else 
            $this->_user->level = 0;
        
        $this->_retailroot = '';
        $this->_exporoot = '/wwwroot/htdocs/dporssi/htdocs2';
        $this->_saferoot = '/wwwroot/htdocs/dporssi/dporssi_data';
        
        
        $this->_document = new Lead_Document();
        
    }
    
    
    /**
     * Returns DB reference
     *
     * @return Zend_Db_Adapter_Pdo_Mysql
     */
    public function getDb()
    {
        return $this->_db;
    }
    
    
    /**
     * Returns EDO_Handler reference
     *
     * @return Lead_EDO_Handler_Functional
     */
    public function getEdo()
    {
        return $this->_edo;
    }
    
    
    public function getRetailRoot()
    {
        return $this->_retailroot;
    }

    public function getSafeRoot()
    {
        return $this->_saferoot;
    }
    
    
    public function getExpoRoot()
    {
        return $this->_exporoot;
    }

    
    
    public function getUser()
    {
        return $this->_user;
    }
    
           
    
    
    
    public function run()
    {
               
        Zend_Loader::loadClass('Zend_Controller_Router_Rewrite');
        $router = new Zend_Controller_Router_Rewrite();
        $router->setParam('useModules', false);

 
        
        
        $router->addRoute('basic2', new Zend_Controller_Router_Route(':controller/:action/:id/:subid'));
        $router->addRoute('basic2', new Zend_Controller_Router_Route(':controller/:action/:id'));
          $router->addRoute('restinpeace', new Zend_Controller_Router_Route('dictator/:id', array('controller' => 'dictator', 'action' => 'index')));
        // $router->addRoute('basic2', new Zend_Controller_Router_Route(':langid/:controller/:action/:id', array('action' => 'index', 'id' => 0), array('langid' => '[a-z]{2}')));
               
        
        
        $front = Zend_Controller_Front::getInstance();
        $front->setControllerDirectory('/wwwroot/LEAD/library/Led/Controllers');
        $front->throwExceptions(true);

        $front->returnResponse(true);
        $front->setRouter($router);
        
        $dispatcher = $front->getDispatcher();
        

        // $dispatcher = $front->getDispatcher();
        
        
        $response = $front->dispatch();
            
                
        return $response->getBody();

    }
    
    
    public function getAppid()
    {
        return $this->_appid;
    }
    
    
    public function getLangid()
    {
        return $this->_langid;
    }
    
    
    public function getPageid()
    {
        return $this->_page->id;
    }
    
    
    public function setLangid($langid)
    {
        $this->_langid = $langid;
    }
    
    
    public function setPage($page)
    {
        $this->_page = $page;
    }
    
    
    public function getPage()
    {
        return $this->_page;
    }
    
    
    public function getDocument()
    {
        return $this->_document;
    }
    
    
    public function getRoute()
    {
        static $route = array();
        if(!$route) {
            
            $page = $this->getPage();   
            
            array_unshift($route, $page->id);
            
            $count = 0;
            
            $parentid = $page->id;
            
            while($parentid = $this->getDB()->fetchOne("SELECT parent_id FROM led_tree WHERE id = {$parentid}")) {
                
                $count++;
                
                array_unshift($route, $parentid);    
                
                if($count > 10) {
                    print_r($route);
                    die();
                }
                    
            }
                        
                       
            
        }
        
        
        return $route;
        
    }

    
    
}